<template>
    <view class="content">
		<!-- 这是一个pda广播扫描插件,引用他!按下pda的SCAN扫描,引用此页面则会监听到 -->
	</view>
</template>

<script>
export default {
    data() {
        return {
            userCode: null
        }
    },
    created: function(option) {
		let that = this;
        that.bindUserCode();
    },
    
    methods: {
		
        //打开扫描头的方法
        bindUserCode() {
			let that = this;
        	that.initScan();
        	that.startScan();
        },
        initScan() {
			 // #ifdef APP-PLUS

        	let that = this;
			
				//获取Android主Activity
				that.main = plus.android.runtimeMainActivity();
				//获取Android意图类
				let Intent = plus.android.importClass('android.content.Intent');
				//实例化意图
				let intent = new Intent();
				//定义意图，模拟按下L键，L键实际上是打开激光的物理键映射，由厂商提供
				intent.setAction('com.android.action.keyevent.KEYCODE_KEYCODE_SCAN_L_DOWN');
				//广播这个意图
				that.main.sendBroadcast(intent);
				//获取Android意图过滤类
				let IntentFilter = plus.android.importClass('android.content.IntentFilter');
				//实例化意图过滤
				that.filter = new IntentFilter();
				//获取扫码成功的意图广播
				that.filter.addAction('com.android.server.scannerservice.broadcast');
				that.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getStringExtra('scannerdata');
						//成功输出扫码内容
						console.log("my-scan输出扫码内容=>"+code);
						that.userCode = code
						// that.resultCode(code)
						that.$emit('scanCode',code)
					}
				});
			
        	// #endif

        },
        startScan() {
			 // #ifdef APP-PLUS
        	this.main.registerReceiver(this.receiver, this.filter);
			// #endif
        },
		// #ifdef APP-PLUS
		// #endif
    }
}
</script>
